﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GRC_TPE_WebApp.Controller;

namespace GRC_TPE_WebApp.View.Private.Client
{
    public partial class ListClients : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Méthode déclenchée au clic un bouton de modification
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Update_button_Click(object sender, EventArgs e)
        {
            String test = (String)((Button)sender).CommandArgument;
            Response.Redirect("EditClient.aspx?id=" + test);
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le lien d'ajout d'un client
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LB_NewClient_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddClient.aspx");
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de filtrage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_Filter_Click(object sender, EventArgs e)
        {
            bool filtered = false;

            ///suppression de tous les paramètres existants
            this.DS_Clients.WhereParameters.Clear();

            ///ajout du filtre sur le nom
            if (!string.IsNullOrEmpty(this.TB_FilterName.Text))
            {
                this.DS_Clients.Where = "it.Name LIKE '%' + @CurrentName + '%'";
                this.DS_Clients.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.String, Name = "CurrentName", DefaultValue = this.TB_FilterName.Text });
                filtered = true;
            }

            ///ajout du filtre sur l'activité
            if (!string.IsNullOrEmpty(this.TB_FilterActivity.Text))
            {
                if (filtered)
                    this.DS_Clients.Where += " AND ";
                this.DS_Clients.Where += "it.Activity LIKE '%' + @CurrentActivity + '%'";
                this.DS_Clients.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.String, Name = "CurrentActivity", DefaultValue = this.TB_FilterActivity.Text });
                filtered = true;
            }

            ///ajout du filtre sur la ville
            if (!string.IsNullOrEmpty(this.TB_FilterCity.Text))
            {
                if (filtered)
                    this.DS_Clients.Where += " AND ";
                this.DS_Clients.Where += "it.Adresse LIKE '%' + @CurrentCity + '%'";
                this.DS_Clients.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.String, Name = "CurrentCity", DefaultValue = this.TB_FilterCity.Text });
                filtered = true;
            }

            string test = this.DS_Clients.Where;
        }

        /// <summary>
        /// Méthode déclenchée au clic sur un bouton de display
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_Display_Click(object sender, EventArgs e)
        {
            String test = (String)((Button)sender).CommandArgument;
            Response.Redirect("DisplayClient.aspx?id=" + test);
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de suppression
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_Delete_Click(object sender, EventArgs e)
        {
            String ids = (String)((Button)sender).CommandArgument;
            int id = int.Parse(ids);
            if (ClientController.DeleteClient(id) == "No error")
                Response.Redirect("ListClients.aspx");
        }
    }
}